package com.fmsz.xmly.adapters.findrecom;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.activitys.findrecom.ActionActivity;
import com.fmsz.xmly.activitys.findrecom.AlbumDetailActivity;
import com.fmsz.xmly.activitys.findrecom.CircleActivity;
import com.fmsz.xmly.activitys.findrecom.HotNowActivity;
import com.fmsz.xmly.activitys.findrecom.WebActivity;
import com.fmsz.xmly.entitys.findrecom.VP2ChildEntity;
import com.fmsz.xmly.entitys.findrecom.VP2DataEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */

public class FindRecomVP2Adapter extends RecyclerView.Adapter<FindRecomVP2Adapter.ViewHolder>{
    private Context context;
    private List<VP2ChildEntity> list;
    private LayoutInflater inflater;

    public FindRecomVP2Adapter(Context context, List<VP2ChildEntity> vp2ChildList) {
        this.context = context;
        this.list = vp2ChildList;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public FindRecomVP2Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.find_recom_vp2_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(FindRecomVP2Adapter.ViewHolder holder, final int position) {
        holder.text.setText(list.get(position).getTitle());
        if (list.get(position).getCoverPath()!=null) {
            Picasso.with(context).load(list.get(position).getCoverPath()).into(holder.image);
            holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    chooseVP2Type(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView image;
        private TextView text;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView)itemView.findViewById(R.id.find_recom_vp2_item_iv);
            text = (TextView)itemView.findViewById(R.id.find_recom_vp2_item_tv);
        }
    }
    //界面跳转
    private void startIntent(Class<?> act, String data) {
        Intent intent = new Intent(context, act);
        intent.putExtra("data", data);
        context.startActivity(intent);
    }
    private void startIntent(Class<?> act,String title,String contentType,String key) {
        Intent intent = new Intent(context, act);
        intent.putExtra("title", title);
        intent.putExtra("contentType", contentType);
        intent.putExtra("key", key);
        context.startActivity(intent);
    }
    //VP2选择要传递的数据类型
    public void chooseVP2Type(int position){
        VP2ChildEntity entity = list.get(position);
        String title = entity.getTitle();
        String type = entity.getContentType();
        if (type.equals("album_detail")) {
            String albumId = entity.getProperties().getAlbumId();
            startIntent(AlbumDetailActivity.class, albumId);
        } else if (type.equals("ranking_list")) {
            String contentType = entity.getProperties().getContentType();
            String key = entity.getProperties().getKey();
            startIntent(HotNowActivity.class, title, contentType, key);
        } else if (type.equals("html5.mall") || type.equals("html5")) {
            String url = entity.getUrl();
            startIntent(WebActivity.class, url);
        } else if (type.equals("activity")) {
            startIntent(ActionActivity.class, null);
        } else if (type.equals("xzone")) {
            startIntent(CircleActivity.class, null);
        }
    }
}
